//
//  SEGUserFeedbackViewController.m
//  ExcellentHome
//
//  Created by liangxinqiang on 14-12-4.
//  Copyright (c) 2014年 陈区. All rights reserved.
//

#import "SEGFeedbackDetailViewController.h"

#import "SEGNewFeedebackViewController.h"
#import "SEGUserFeedbackViewController.h"
#import "SEGUserFeedbackViewModel.h"

@interface SEGUserFeedbackViewController ()
@property(nonatomic, strong) SEGUserFeedbackViewModel *viewModel;
@end

@implementation SEGUserFeedbackViewController
@synthesize contextField;

- (void)loadView
{
    UIScreen* scr = [UIScreen mainScreen];

    CGRect appbounds = CGRectMake(0, 0, scr.bounds.size.width, scr.bounds.size.height - kNavigationBarAndStatusBarHeight);
    UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appbounds.size.width, appbounds.size.height)];

    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, appbounds.size.width, appbounds.size.height) style:UITableViewStylePlain];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    m_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    m_tableView.backgroundColor = [UIColor clearColor];
    [mainView addSubview:m_tableView];

    self.view = mainView;
    
    [self bindViewModel];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = self.title.length >0?self.title : @"产品反馈";
    context = @"";
    [self showCustomNavigationBackButton];
    [self showCustomNavigationRightButtonWithTitle:@"新反馈" image:[UIImage imageWithContentsOfFile:SEGPicName(@"")] hightlightImage:[UIImage imageWithContentsOfFile:SEGPicName(@"")] color:kColor_T];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self requestFeedbackList];
}

#pragma mark - 新网络请求
- (void)requestFeedbackList {
    [self.viewModel requestFeedbackList:^(NSString * _Nonnull code) {
        if ([code isEqualToString:@"0"]) {
            if ([self.viewModel.listData count] <= 0) {
                NSDictionary *params = @{@"view":self.view};
                [[SEGUhomeMediator sharedInstance]seg_showCustomProgressImage:params];
            } else {
                NSDictionary *params = @{@"view":self.view};
                [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:params];
            }
            [self->m_tableView reloadData];
        }
    }];
}

#pragma mark - tableview delegate

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.listData.count;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{

    return 65;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString* cellIdStr = @"Cell";
    SEGBaseTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdStr];
    if (!cell) {
        cell = [[SEGBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdStr];
        cell.backgroundColor = kColor_W;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 64.5, CGRectGetWidth(self.view.bounds), 0.5)];
        line.backgroundColor = kColor_L2;
        [cell.contentView addSubview:line];

        UILabel* title = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, LYScreenWidth()-40, 20)];
        title.tag = 1001;
        title.textColor = kColor_1;
        title.font = [UIFont systemFontOfSize:16];
        [cell.contentView addSubview:title];
        

        UILabel* time = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, LYScreenWidth()-40, 10)];
        time.textColor = kColor_3;
        time.tag = 1002;
        time.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:time];
        
    }

    UILabel* title = (UILabel*)[cell.contentView viewWithTag:1001];
    UILabel* time = (UILabel*)[cell.contentView viewWithTag:1002];

    title.text = [[self.viewModel.listData objectAtIndex:indexPath.row] objectForKey:@"context"];
    time.text = [[self.viewModel.listData objectAtIndex:indexPath.row] objectForKey:@"createDate"];

    return cell;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    SEGFeedbackDetailViewController* detail = [[SEGFeedbackDetailViewController alloc] init];
    detail.hidesBottomBarWhenPushed = YES;
    detail.sugId = [[self.viewModel.listData objectAtIndex:indexPath.row] objectForKey:@"sugId"];
    [self.navigationController pushViewController:detail animated:YES];
    
}
#pragma mark - Button事件

//右侧按钮
- (void)onNavigationRightButtonClicked:(UIButton*)sender
{
    SEGNewFeedebackViewController* newFeedebackView = [[SEGNewFeedebackViewController alloc] init];
    newFeedebackView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:newFeedebackView animated:YES];
    
}

#pragma mark - TextField Delegate

- (void)textFieldDidEndEditing:(UITextField*)textField
{
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [self hideKeyBoard];

    return YES;
}

- (void)hideKeyBoard
{
    [contextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - init Views
-(void)segInitViews{

}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGUserFeedbackViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGUserFeedbackViewModel alloc]init];
    }
    return _viewModel;
}
@end
